////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////

package org.teotigraphix.swizplugin.bundles
{

import org.swizframework.core.ISwiz;
import org.teotigraphix.swizplugin.services.IBundleService;

/**
 * The <strong>SwizContext</strong> is the default implementation 
 * of <code>ISwizContext</code>.
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @productversion 1.0
 */
public class SwizContext implements ISwizContext
{	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Variables
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	protected var service:IBundleService;
	
	//--------------------------------------------------------------------------
	//
	//  IBundleContext API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  activator
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _activator:IBundleActivator;
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.IBundleContext#activator
	 */
	public function get activator():IBundleActivator
	{
		return _activator;
	}
	
	//----------------------------------
	//  bundle
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _bundle:BundleProxy;
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.IBundleContext#bundle
	 */
	public function get bundle():IBundle
	{
		return _bundle;
	}	
	
	//--------------------------------------------------------------------------
	//
	//  ISwizContext API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  swiz
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _swiz:ISwiz;
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.ISwizContext#swiz
	 */
	public function get swiz():ISwiz
	{
		return _swiz;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	public function SwizContext(bundle:BundleProxy, swiz:ISwiz)
	{
		super();
		
		_bundle = bundle;
		_swiz = swiz;
	}
	
	//--------------------------------------------------------------------------
	//
	//  IBundleContext API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.IBundleContext#getBundle()
	 */
	public function getBundle(id:int):IBundle
	{
		return service.getBundle(id);
	}
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.IBundleContext#getBundles()
	 */
	public function getBundles():Array
	{
		return service.getBundles();
	}
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.IBundleContext#close()
	 */
	public function close():void
	{
	}
	
	//--------------------------------------------------------------------------
	//
	//  ISwizContext API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.ISwizContext#getDefinition()
	 */
	public function getDefinition(className:String):Object
	{
		return _swiz.domain.getDefinition(className);
	}
	
	/**
	 * @copy org.teotigraphix.swizplugin.bundles.ISwizContext#hasDefinition()
	 */
	public function hasDefinition(className:String):Boolean
	{
		return _swiz.domain.hasDefinition(className);
	}
	
	//--------------------------------------------------------------------------
	//
	//  Internal :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 * @throws TypeError TypeError: Error #1007.
	 * @throws BundleError Bundle cannot be loaded.
	 * @throws Error Bundle's activator threw the error.
	 */
	internal function load():void
	{
		// this calls loadClassImmediate()
		// the activator has to be in the bundle
		try {
			_activator = _bundle.loadBundleActivator();
		} catch (te:TypeError) {
			throw te; // try to create the class, loading, push back to bundle.start()
		} catch (e:Error) {
			var be:BundleError = new BundleError( "Bundle cannot be loaded [" + _bundle.symbolicName + "]" );
			be.originalError = e;
			throw be;
		}
	}
	
	/**
	 * @private
	 */
	internal function start():void
	{
		if (_activator is IBundleActivator)
		{
			try {
				startActivator(_activator);
			}
			catch (e:Error)	{
				// the bundle's activator threw the error
				_activator = null;
				throw e;
			}
		}	
	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	protected function startActivator(activator:IBundleActivator):void
	{
		_activator.start(IBundleContext(this));
	}
}
}